package com.ztesoft.web.common.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ztesoft.core.common.Page;
import com.ztesoft.framework.exception.BaseAppException;
import com.ztesoft.framework.log.ZTEsoftLogManager;
import com.ztesoft.web.common.db.po.SysModuleNodePO;
import com.ztesoft.web.common.db.po.SysModulePO;
import com.ztesoft.web.common.service.ISysModuleService;

/**
 * <Description>系统模块 <br>
 * 
 * @author lin.jie <br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2015年8月24日 <br>
 * @since V1.0<br>
 * @see com.ztesoft.web.common.controller <br>
 */

@Controller
@RequestMapping("/common/sysModule")
public class SysModuleController {
	 private static final ZTEsoftLogManager logger = ZTEsoftLogManager
	            .getLogger(SysModuleController.class);
	@Autowired
	private ISysModuleService sysModuleService;

	@RequestMapping("index")
	public String index(Model model) {
		return "baseconfig/jsp/dmPlan";
	}

	@RequestMapping("queryAllForTree")
	@ResponseBody
	public SysModuleNodePO[] queryAllForTree() throws BaseAppException {
		SysModuleNodePO[] resultArray = sysModuleService.queryAllForTree();
		return resultArray;
	}
	@RequestMapping("queryRecordByPage")
    @ResponseBody
    public Page<SysModulePO> queryRecordByPage(SysModulePO record,
            Page<SysModulePO> resultPage) throws BaseAppException {
        resultPage = sysModuleService.selectByArgAndPage(record, resultPage);
        return resultPage;
    }
	//系统模块维护查询
	@RequestMapping("queryRecordByPageSys")
	@ResponseBody
	public Page<SysModulePO> queryRecordByPageSys(SysModulePO record,
			Page<SysModulePO> resultPage) throws BaseAppException {
		resultPage = sysModuleService.selectByArgAndPageSys(record, resultPage);
		return resultPage;
	}
	//系统模块维护 添加
    @RequestMapping("add")
    @ResponseBody
    public SysModulePO add(SysModulePO record) throws BaseAppException {
        logger.debug("add record begin...record=[{0}]", record);
        sysModuleService.add(record);
        return record;
    }
  //系统模块维护 修改
    @RequestMapping("update")
    @ResponseBody
    public SysModulePO update(SysModulePO record) throws BaseAppException {
        logger.debug("modify record begin...record=[{0}]", record);
        sysModuleService.update(record);
        return record;
    }
  //系统模块维护 删除
    @RequestMapping("delete")
    @ResponseBody
    public int delete(SysModulePO record) throws BaseAppException {
        logger.debug("delete record begin...record=[{0}]", record);
        return sysModuleService.delete(record);
    }

	@RequestMapping("isExistProcedureInfo")
	@ResponseBody
	public boolean isExistProcedureInfo(@RequestParam(value = "dbLinkId", required = true) Integer dbLinkId, 
			@RequestParam(value = "dbUserName", required = true) String dbUserName, @RequestParam(value = "procedureName", required = true)String procedureName)
			throws Exception {
		if(dbLinkId!=0&&!"".equals(dbUserName) && !StringUtils.isEmpty(procedureName)){
			return sysModuleService.isExistProcedure(dbLinkId, dbUserName, procedureName);
		}
		return false;
	}
	
	@RequestMapping("isExistPartitions")
	@ResponseBody
	public boolean isExistPartitions(
			@RequestParam(value = "dbLinkId", required = true) Integer dbLinkId, 
			@RequestParam(value = "dbUserName", required = true) String dbUserName,
			@RequestParam(value = "tableName", required = true)String tableName)
					throws Exception {
		if(dbLinkId!=0&&!"".equals(dbUserName) && !StringUtils.isEmpty(tableName)){
			return sysModuleService.isExistPartitions(dbLinkId, dbUserName, tableName);
		}
		return false;
	}
	
//	@RequestMapping("qryProcedureInfo")
//	@ResponseBody
//	public ProcedureDataPO[] qryProcedureInfo(@RequestParam(value = "dbLinkId", required = true) Integer dbLinkId, 
//			@RequestParam(value = "dbUserName", required = true) String dbUserName, @RequestParam(value = "procedureName", required = true)String procedureName)
//					throws Exception {
//		ProcedureDataPO[] resultArray = null;
//		if(dbLinkId!=0&&!"".equals(dbUserName) && StringUtils.isEmpty(procedureName)){
//			resultArray  = sysModuleService.queryAllProForDbLinkId(dbLinkId, dbUserName);
//		}
//		return resultArray;
//	}
	
	@RequestMapping("qryInfoByDbLinkId")
	@ResponseBody
	public  Map<String, String> qryInfoByDbLinkId(@RequestParam(value = "dbLinkId", required = true) Integer dbLinkId)
			throws Exception {
			Map<String, String> resMap= new HashMap<String, String>();
			resMap  = sysModuleService.qryInfoByDbLinkId(dbLinkId);
		return resMap;
	}
    @RequestMapping("qryRecordInfo")
    @ResponseBody
    public SysModulePO qryRecordInfo(@RequestParam(value = "moduleId",
            required = true) Integer moduleId) throws BaseAppException {
        SysModulePO record = sysModuleService.selectByPrimaryKey(moduleId);
        return record;
    }
	
}
